package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherNoticeMapper;
import com.zjdiepu.www.ibiz.other.IOtherNoticeService;
import com.zjdiepu.www.model.other.dto.OtherNotice;
import com.zjdiepu.www.model.other.ext.OtherNoticeExt;

/**
 * 公告Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年06月09日 18时59分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherNoticeServiceImpl extends BaseService implements IOtherNoticeService {
	public static final Logger logger = LoggerFactory.getLogger(OtherNoticeServiceImpl.class);

	@Autowired
	private OtherNoticeMapper otherNoticeMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherNotice otherNotice, OperateType operType) {
		switch (operType) {
		case add:
			otherNotice.initInsertData(otherNotice.getModifier(), otherNotice.getModifierName());
			otherNoticeMapper.insert(otherNotice);
			break;
		case edit:
			otherNotice.initUpdateData(otherNotice.getModifier(), otherNotice.getModifierName());
			// 影响的记录数
			int updateCount = otherNoticeMapper.updateByPrimaryKeySelective(otherNotice);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherNotice.initDeleteData(otherNotice.getModifier(), otherNotice.getModifierName());
			// 影响的记录数
			int deleteCount = otherNoticeMapper.updateByPrimaryKeySelective(otherNotice);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherNotice> selectByExt(OtherNoticeExt otherNoticeExt) {
		return otherNoticeMapper.selectByExt(otherNoticeExt);
	}
	
	@Override
	public int countByExt(OtherNoticeExt otherNoticeExt){
		return otherNoticeMapper.countByExt(otherNoticeExt);
	}
	
	@Override
	public OtherNotice selectById(String id) {
		return otherNoticeMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherNoticeMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherNotice otherNotice) {
		return otherNoticeMapper.countRefined(otherNotice);
	}

	@Override
	public OtherNotice selectRefined(OtherNotice otherNotice) {
		return otherNoticeMapper.selectRefined(otherNotice);
	}
	
	@Override
	public int deletedRefined(OtherNotice otherNotice) {
		return otherNoticeMapper.deletedRefined(otherNotice);
	}

	@Override
	public boolean repeat(OtherNotice otherNotice) {
		int count = countRefined(otherNotice);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherNotice.getId())) {
			return true;
		}
		OtherNotice dbItem = selectRefined(otherNotice);
		if(null == dbItem) {
			return false;
		}
		return !otherNotice.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherNoticeMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherNotice> otherNotices) {
		return otherNoticeMapper.insertBatch(otherNotices);
	}
}
